package org.mxp.demo2;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.mxp.utils.RabbitMQUtils;

import java.util.Date;

public class ReceiveObj {
    public static void main(String[] args) throws Exception {
        // 获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 获取当前时间，区分不同线程
        Date now = new Date();
        // 接收到消息回调
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(now.getTime() + "，接受到消息：" + message);
        };
        // 取消消息时回调
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息消费被中断");
        };
        // 接受消息
        channel.basicConsume("demo2", true, deliverCallback, cancelCallback);
    }
}
